package com.breadenglish.cms.api.app.gateway.util;

import org.apache.commons.lang3.StringEscapeUtils;
import org.springframework.web.bind.WebDataBinder;

import java.beans.PropertyEditorSupport;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by 坤 on 14-3-15.
 */
public class NumberFormatUtil {
    public static void registerAllFormat(WebDataBinder binder) {
        binder.registerCustomEditor(Date.class, new CustomerDateEditor());
        binder.registerCustomEditor(Integer.TYPE, new CustomerIntegerEditor());
        binder.registerCustomEditor(Float.TYPE, new CustomerFloatEditor());
        binder.registerCustomEditor(Double.TYPE, new CustomerDoubleEditor());
        binder.registerCustomEditor(String.class, new CustomerStringEditor());
    }

    public static void registerAllFormatExcludeDate(WebDataBinder binder) {
        binder.registerCustomEditor(Integer.TYPE, new CustomerIntegerEditor());
        binder.registerCustomEditor(Float.TYPE, new CustomerFloatEditor());
        binder.registerCustomEditor(Double.TYPE, new CustomerDoubleEditor());
        binder.registerCustomEditor(String.class, new CustomerStringEditor());
    }

    public static void registerDateFormat(WebDataBinder binder) {
        binder.registerCustomEditor(Date.class, new CustomerDateEditor());
    }

    public static void registerDateTimeFormat(WebDataBinder binder) {
        binder.registerCustomEditor(Date.class, new CustomerDateTimeEditor());
    }

    public static void registeStringFormat(WebDataBinder binder) {
        binder.registerCustomEditor(Date.class, new CustomerStringEditor());
    }

    private static class CustomerFloatEditor extends PropertyEditorSupport {
        public String getAsText() {
            Float t = (Float) getValue();
            return t.toString();
        }

        public void setAsText(String str) {
            if (str == null || str.length() == 0)
                setValue(Float.valueOf(0));
            else {
                try {
                    setValue(Float.parseFloat(str));
                } catch (Exception ex) {
                    setValue(Float.valueOf(0));
                }
            }
        }
    }

    private static class CustomerIntegerEditor extends PropertyEditorSupport {
        public String getAsText() {
            Integer t = (Integer) getValue();
            return t.toString();
        }

        public void setAsText(String str) {
            if (str == null || str.length() == 0)
                setValue(Integer.valueOf(0));
            else {
                try {
                    setValue(Integer.parseInt(str));
                } catch (Exception ex) {
                    setValue(Integer.valueOf(0));
                }
            }
        }
    }

    private static class CustomerDoubleEditor extends PropertyEditorSupport {
        public String getAsText() {
            Double t = (Double) getValue();
            return t.toString();
        }

        public void setAsText(String str) {
            if (str == null || str.length() == 0)
                setValue(Double.valueOf(0));
            else {
                try {
                    setValue(Double.parseDouble(str));
                } catch (Exception ex) {
                    setValue(Double.valueOf(0));
                }
            }
        }
    }

    private static class CustomerDateEditor extends PropertyEditorSupport {
        public String getAsText() {
            Date d = (Date) getValue();
            if (d == null)
                return "";
            return new SimpleDateFormat("yyyy-MM-dd").format(d);
        }

        public void setAsText(String str) {
            if (str != null)
                try {
                    setValue(new SimpleDateFormat("yyyy-MM-dd").parse(str));
                } catch (ParseException e) {
                    try {
                        setValue(new SimpleDateFormat("yyyy-MM").parse(str));
                    } catch (ParseException e1) {
                        setValue(null);
                    }
                }
        }
    }

    private static class CustomerDateTimeEditor extends PropertyEditorSupport {
        public String getAsText() {
            Date d = (Date) getValue();
            if (d == null)
                return "";
            return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(d);
        }

        public void setAsText(String str) {
            if (str != null)
                try {
                    setValue(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
                            .parse(str));
                } catch (ParseException e) {
                    setValue(null);
                }
        }
    }

    private static class CustomerStringEditor extends PropertyEditorSupport {
        private boolean escapeHTML = false;
        private boolean escapeJavaScript = false;
        private boolean escapeSQL = false;

        public String getAsText() {
            String d = (String) getValue();
            if (d == null)
                return "";
            return d;
        }

        public void setAsText(String text) {
            if (text == null) {
                setValue(null);
            } else {
                String value = text.trim();
                if (convertToDate(value) != null) {
                    setValue(value);
                    return;
                }
                if (escapeHTML) {
                    value = StringEscapeUtils.escapeHtml4(value);
                }
                if (escapeJavaScript) {
                    value = StringEscapeUtils.escapeJava(value);
                }
                if (escapeSQL) {
                    value = StringEscapeUtils.escapeEcmaScript(value);
                }
                setValue(value);
            }
        }
    }

    // List of all date formats that we want to parse.
    // Add your own format here.
    private static List<SimpleDateFormat> dateFormats = new ArrayList<SimpleDateFormat>() {
        {
            add(new SimpleDateFormat("yyyy-MM-dd"));
            add(new SimpleDateFormat("yyyy-MM-dd HH"));
            add(new SimpleDateFormat("yyyy-MM-dd HH:mm"));
            add(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
            add(new SimpleDateFormat("yyyy-MM"));
        }
    };

    /**
     * Convert String with various formats into java.util.Date
     *
     * @param input Date as a string
     * @return java.util.Date object if input string is parsed
     * successfully else returns null
     */
    public static Date convertToDate(String input) {
        Date date = null;
        if (null == input) {
            return null;
        }
        for (SimpleDateFormat format : dateFormats) {
            try {
                format.setLenient(false);
                date = format.parse(input);
            } catch (ParseException e) {
                //Shhh.. try other formats
            }
            if (date != null) {
                break;
            }
        }
        return date;
    }
}
